package com.loma.common.model.http;


import java.io.IOException;
import java.lang.reflect.Type;

import rxhttp.wrapper.annotation.Parser;
import rxhttp.wrapper.entity.ParameterizedTypeImpl;
import rxhttp.wrapper.parse.AbstractParser;

@Parser(name = "Msg" )
public class MsgParser  extends AbstractParser<String>  {

    //注意，以下两个构造方法是必须的
    public MsgParser() { super(); }
    public MsgParser(Type type) { super(type); }

    @Override
    public String onParse(okhttp3.Response response) throws IOException {
        final Type type = ParameterizedTypeImpl.get(Msg.class, mType); //获取泛型类型
        Msg  data = rxhttp.wrapper.utils.Converter.convert(response,type);

        if (data.getCode()!=200) {//这里假设code不等于200，代表数据不正确，抛出异常
            throw new ApiException(data.getCode() , data.getMsg());
        }else {
            return data.getMsg();
        }

    }
}